package com.qianfeng.util;

import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 2019/12/9
 * Administrator
 * springboot1207
 * 面向对象面向君  不负代码不负卿
 */
@Configuration
public class RealmConfig {
 /*  <!--1.创建自定义realm对象-->*/
  @Bean
  public MyRealm createRealm(){
      return new MyRealm();
  }

 /*<!--2.创建SecurityManager-->*/
    @Bean
    public DefaultWebSecurityManager webSecurityManager(){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(createRealm());
        return manager;
    }

    /*<!--3.配置shiro过滤器，id等于web.xml文件中的filter-name-->*/
    @Bean
    public ShiroFilterFactoryBean factoryBean(){
        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        filterFactoryBean.setSecurityManager(webSecurityManager());
        filterFactoryBean.setLoginUrl("/login");
        LinkedHashMap map = new LinkedHashMap();
        map.put("/js/**","anon");
        map.put("/img/**","anon");
        map.put("/css/**","anon");
        map.put("/**","authc");
        filterFactoryBean.setFilterChainDefinitionMap(map);
        return filterFactoryBean;
    }

    //shiro和spring关联，开启shiro注解
    @Bean
    public AuthorizationAttributeSourceAdvisor advisor(){
        AuthorizationAttributeSourceAdvisor sourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        sourceAdvisor.setSecurityManager(webSecurityManager());
        return sourceAdvisor;
    }
    //配置原来在web.xml文件中使用的过滤器对象
    @Bean
    public DefaultAdvisorAutoProxyCreator proxyCreator(){
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setProxyTargetClass(true);
        return autoProxyCreator;
    }

}
